import { REACT_TEXT } from "./constants";

export function wrapToVdom(element) {
  return typeof element === "string" || typeof element === "number"
    ? { type: REACT_TEXT, props: element }
    : element;
}

export function isFunction(value) {
  return typeof value === "function";
}

/**
 * 两个对象的浅比较
 * @param {*} o1
 * @param {*} o2
 * @returns
 */
export function shallowEqual(o1, o2) {
  if (o1 === o2) {
    return true;
  }

  if (
    typeof o1 !== "object" ||
    typeof o2 !== "object" ||
    o1 === null ||
    o2 === null
  ) {
    return false;
  }

  const keys1 = Object.keys(o1);
  const keys2 = Object.keys(o2);

  if (keys1.length !== keys2.length) {
    return false;
  }

  for (const key of keys1) {
    if (!o2.hasOwnProperty(key) || o1[key] !== o2[key]) {
      return false;
    }
  }

  return true;
}
